﻿using Domain.Entitys.Base_Manage;
using Domain.IRepositorys.Base_Manage;
using EFCore.Extention;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

namespace EFCore.Repositorys.Base_Manage
{
    public class UserRepository : IUserRepository
    {
        private readonly MyDbContext myDbContext;

        public UserRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 根据用户名和部门分页查询用户信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dept_id"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<Sys_User>> GetSys_UsersAsync(string name, int dept_id, int currentPage, int pageSize)
        {
            IQueryable<Sys_User> query = myDbContext.Sys_User.Where(u => u.Del_flag == '0');
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(u => u.User_account.Contains(name));
            }
            if (dept_id != 0)
            {
                query = query.Where(u => u.Dept_id == dept_id);
            }
            var users = await query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
            //List<UserWithRoles> userWithRoles = new List<UserWithRoles>();
            //foreach (var user in users)
            //{
            //    //查询出用户对应的角色ID
            //    var userRoles = myDbContext.Sys_User_Role.FirstOrDefault(ur => ur.User_id == user.User_id);
            //    //查询出角色ID对应的角色信息
            //    var roles = myDbContext.Sys_Role.FirstOrDefault(r => r.Role_id == userRoles.Role_id);
            //    var dept = myDbContext.Sys_Dept.FirstOrDefault(d => d.Dept_id == user.Dept_id);
            //    userWithRoles.Add(new UserWithRoles
            //    {
            //        Sys_User = user,
            //        Sys_Roles = roles,
            //        Sys_Dept = dept,
            //    });
            //}
            return users;
        }

        /// <summary>
        /// 根据用户名和部门查询用户总数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public async Task<int> GetSys_UsersCountAsync(string name, int dept_id)
        {
            IQueryable<Sys_User> query = myDbContext.Sys_User.Where(u => u.Del_flag == '0');
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(u => u.User_account.Contains(name));
            }
            if (dept_id != 0)
            {
                query = query.Where(u => u.Dept_id == dept_id);
            }
            return await query.CountAsync();
        }

        /// <summary>
        /// 查询全部负责人
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Sys_User>> GetUsersAsync(int dept, string name)
        {
            List<Sys_User> sys_Users = await myDbContext.Sys_User.ToListAsync();
            if (dept != 0)
            {
                sys_Users = sys_Users.Where(u => u.Dept_id == dept).ToList();
            }
            if (!string.IsNullOrEmpty(name))
            {
                sys_Users = sys_Users.Where(u => u.User_name == name).ToList();
            }
            return sys_Users;
        }

        public async Task<Sys_User> LoginAsync(Sys_User user)
        {
            return await myDbContext.Sys_User.FirstOrDefaultAsync(u => u.User_account == user.User_account && u.User_pwd == user.User_pwd);
            //myDbContext.User.Where(u => u.UserName == user.UserName && u.UserPWD == user.UserPWD).Join(
            //    myDbContext.
            //    )
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="sys_User"></param>
        /// <returns></returns>
        public async Task<int> Sys_UserAddAsync(Sys_User sys_User)
        {
            sys_User.User_pwd = MD5Helper.GetMD5Hash(sys_User.User_pwd);
            await myDbContext.Sys_User.AddAsync(sys_User);
            await myDbContext.SaveChangesAsync();
            return sys_User.User_id;
        }

        /// <summary>
        /// 逻辑删除用户信息
        /// </summary>
        /// <param name="sys_User"></param>
        /// <returns></returns>
        public async Task<int> Sys_UserDeleteAsync(Sys_User sys_User)
        {
            Sys_User sys_User1 = myDbContext.Sys_User.Find(sys_User.User_id);
            sys_User1.Del_flag = sys_User.Del_flag;
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sys_User"></param>
        /// <returns></returns>
        public async Task<int> Sys_UserUpdateAsync(Sys_User sys_User)
        {
            Sys_User sys_User1 = myDbContext.Sys_User.Find(sys_User.User_id);
            //sys_User1.Create_by = sys_User.Create_by;
            //sys_User1.Create_time = sys_User.Create_time;
            sys_User1.User_account = sys_User.User_account;
            sys_User1.User_phone = sys_User.User_phone;
            sys_User1.User_sex = sys_User.User_sex;
            sys_User1.Dept_id = sys_User.Dept_id;
            sys_User1.Update_by = sys_User.Update_by;
            sys_User1.Update_time = sys_User.Update_time;
            myDbContext.Sys_User.Update(sys_User1);
            await myDbContext.SaveChangesAsync();
            return sys_User.User_id;
        }
    }
}
